package com.tianhui.consignortwofei.mvp.ui.activity;

import android.text.TextUtils;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.manager.ActivityManager;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.ToastUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.base.BaseFragment;
import com.tianhui.consignortwofei.mvp.model.UserInfoModel;
import com.xuexiang.xui.widget.edittext.PasswordEditText;
import com.xuexiang.xutil.security.Base64Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/3/30
 **/
public class ChangePasswordActivity extends BaseActivity {


    @BindView(R.id.activity_change_password_oldPasswordEditText)
    PasswordEditText oldPasswordEditText;
    @BindView(R.id.activity_change_password_newPasswordEditText)
    PasswordEditText newPasswordEditText;
    @BindView(R.id.activity_change_password_confirmPasswordEditText)
    PasswordEditText confirmPasswordEditText;

    private UserInfoModel mModel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_change_password;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {

        setTitle("密码修改");
        mModel = new UserInfoModel();
    }

    @Override
    public BaseFragment createFragment() {
        return null;
    }

    @OnClick(R.id.activity_change_password_commitButton)
    public void commit() {

        String oldPassword = oldPasswordEditText.getText().toString().trim();
        String newPassword = newPasswordEditText.getText().toString().trim();
        String confirmPassword = confirmPasswordEditText.getText().toString().trim();


        if (TextUtils.isEmpty(oldPassword)) {
            ToastUtil.showShortToast("请输入旧密码");
            return;
        }
        if (oldPassword.length() < 8) {
            ToastUtil.showShortToast("旧密码不能少与8位");
            return;
        }
        if (TextUtils.isEmpty(newPassword)) {
            ToastUtil.showShortToast("请输入新密码");
            return;
        }
        if (newPassword.length() < 8) {
            ToastUtil.showShortToast("新密码不能少与8位");
            return;
        }

        if (TextUtils.isEmpty(confirmPassword)) {
            ToastUtil.showShortToast("请输入确认密码");
            return;
        }
        if (confirmPassword.length() < 8) {
            ToastUtil.showShortToast("确认密码不能少与8位");
            return;
        }

//        if (!CheckUtil.isPasswordStrength(newPassword)) {
//            ToastUtil.showShortToast("新密码必须包含字母和数字");
//            return;
//        }
//        if (!CheckUtil.isPasswordStrength(confirmPassword)) {
//            ToastUtil.showShortToast("确认密码必须包含字母和数字");
//            return;
//        }

        if (!newPassword.equals(confirmPassword)) {
            ToastUtil.showShortToast("两次密码输入不一致");
            return;
        }


        Map<String, String> map = new HashMap<>();

//        if (BuildConfig.FLAVOR.equals("beta")) {
        map.put("oldPassword", Base64Utils.encode(oldPassword));
        map.put("password", Base64Utils.encode(newPassword));
        map.put("rePassword", Base64Utils.encode(confirmPassword));
//        } else {
//            map.put("oldPassword", oldPassword);
//            map.put("password", newPassword);
//            map.put("rePassword", confirmPassword);
//        }

        requestChangePassword(map);
//        new AuthCode(this).authCode(new AuthCode.OnAuthCodeCallback() {
//            @Override
//            public void onAuthCallback(CaptchaInfo info) {
//                if (info.ret == 0){
//
//                }
//            }
//        });


    }

    private void requestChangePassword(Map<String, String> map) {
        mModel.changePasswordModel(this, map, true, true, this.bindToLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                showToast(commonResponse.getMsg());
                if (commonResponse.getCode() == 20000) {
                    ActivityManager.getInstance().finishAllActivity();
                    openActivity(LoginActivityNew.class);
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }


}
